package com.xr.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.xr.constant.MessageConstant;
import com.xr.constant.RedisConstant;
import com.xr.constant.RedisMessageConstant;
import com.xr.pojo.Order;
import com.xr.result.Result;
import com.xr.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.Map;

@RestController
@RequestMapping("orderMobile")
public class OrderMobileController {

    @Reference
    private OrderService orderService;

    @Autowired
    private JedisPool jedisPool;


    //    提交预约
    @RequestMapping("/submit")
    public Result submit(@RequestBody Map<String,String> map) {
        String telephone = (String) map.get("telephone");
//      从Redis中获取验证码
        String codeInRedis = jedisPool.getResource().get(telephone + "_" + RedisMessageConstant.SENDTYPE_ORDER);
        System.out.println("RedisCode:" + codeInRedis);
//      从页面获取验证码
        String codeInWeb = (String) map.get("validateCode");
        System.out.println("codeInWeb:" + codeInWeb);
//      将用户输入的验证码和Redis中的验证码进行比对
//        System.out.println(codeInRedis==codeInWeb);
//        System.out.println(codeInRedis.equals(codeInWeb));
        if (codeInRedis != null && codeInWeb != null && codeInRedis.equals(codeInWeb)) {
            //比对成功 进行预约
            //设置预约类型
            map.put("orderType", "微信预约");
//            System.out.println("join 预约");
            //调用服务实现在线预约
            Result result = orderService.order(map);
            return result;
        } else {
            //不成功
            return new Result(false, MessageConstant.VALIDATECODE_ERROR);
        }
    }


    //根据id查询预约info
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            Map map = orderService.findById(id);
            System.out.println("mapValue:"+map);
            return new Result(true,MessageConstant.QUERY_ORDER_SUCCESS,map);
        }catch (Exception e){
            e.printStackTrace();
            return  new Result(false,MessageConstant.QUERY_ORDER_FAIL);
        }
    }
}
